<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/utils/request.js}"></script>
    <style>
        body {
            display: flex;
            justify-self: center;
            align-items: center;
            width: 100%;
            height: 100vh;
            margin: 0;
        }
        .login_plane {
            width: 500px;
            margin: 0 auto;
            border: 1px solid #d5ebff;
            border-radius: 10px;
            display: flex;
            justify-self: center;
            flex-direction: column;
            align-items: center;
        }
        .login_plane>div:nth-last-child(1) {
            margin: 20px 0;
        }
        .login_plane>div:nth-last-child(2)>div {
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="login_plane"></div>
</body>
<script type="text/javascript">
const login_plane = document.querySelector(".login_plane")
const templates = [
    `
        <h1>登录</h1>
        <div>
            <div placeholder="username">
                <label>账号: </label>
                <input type="text" placeholder="请输6-18长度" />
            </div>
            <div placeholder="password">
                <label>密码: </label>
                <input type="password" placeholder="请输6-18长度" />
            </div>
        </div>
        <div>
            <button onclick="commit(0)">提交</button>
            <button onclick="switch_login_regis(1)">注册</button>
        </div>
    `,
    `
        <h1>注册</h1>
        <div>
            <div placeholder="username">
                <label>账号: </label>
                <input type="text" placeholder="请输6-18长度" />
            </div>
            <div placeholder="password">
                <label>密码: </label>
                <input type="password" placeholder="请输6-18长度" />
            </div>
            <div placeholder="nickname">
                <label>昵称: </label>
                <input type="text" />
            </div>
            <div placeholder="gender">
                <label>性别: </label>
                <label>不愿透露</label>
                <input name="sex" type="radio" value="0" checked />
                <label>男</label>
                <input name="sex" type="radio" value="1" />
                <label>女</label>
                <input name="sex" type="radio" value="2" />
            </div>
            <div placeholder="phone">
                <label>手机号: </label>
                <input type="text" />
            </div>
            <div placeholder="email">
                <label>邮箱: </label>
                <input type="text" />
            </div>
        </div>
        <div>
            <button onclick="commit(1)">提交</button>
            <button onclick="switch_login_regis(0)">登录</button>
        </div>
    `
]
login_plane.innerHTML = templates[0]

/**
 * 得到错误信息
 * @param msg 错误信息
 * @returns {{msg}} 返回错误类, 检查是否有这个属性就可以判断他是不是一个错误的
 */
const get_error = (msg) => {
    return {
        msg: msg
    };
}

/**
 * 切换登录注册页面
 * @param template_number 登录注册页面编号
 */
const switch_login_regis = (template_number) => {
    login_plane.innerHTML = templates[template_number]
}

/**
 * 得到性别信息
 * @param dom 装有性别的dom
 * @returns {number|*} 返回的性别编码
 */
const get_gender = (dom) => {
    for (let child of dom.children) {
        if (child.checked) {
            return child.value
        }
    }
    return 0
}

/**
 * 通过正则来检查字符串是否正确
 * @param text 待检查的文本
 * @param rag 正则表达式
 * @param msg 错误提示
 * @returns {{msg}|*} 正确则返回原内容, 错误则返回错误信息
 */
const check_correct = (text, rag, msg) => {
    if(text.search(rag) === -1) {
        return get_error(msg)
    }
    return text
}

/**
 * 得到表单内容
 * @returns {Object|{}} 返回表单,或者是错误信息
 */
const get_form_data = () => {
    const form = login_plane.children[1].children
    const data = {}
    let value = null
    for (let form_item of form) {
        const attribute = form_item.getAttribute('placeholder')
        value = null
        const valid = form_item.children[1].value

        switch (attribute) {
            case 'username':
            case 'password':
                value = check_correct(valid, /^[a-zA-z0-9]{6,18}$/, '账号或者密码长度不正确')
                break
            case 'nickname':
                value = valid
                break
            case 'gender':
                value = get_gender(form_item)
                break;
            case 'phone':
                if (valid != null && valid !== '') {
                    value = check_correct(valid, /^1\d{10}$/, '手机号输入错误')
                }
                break
            case 'email':
                if (valid != null && valid !== '') {
                    value = check_correct(valid, /^[^@.]\w*@\w+(\.\w+)+$/, '请输入正确的邮箱')
                }
                break
        }


        if (value instanceof Object) {
            return value
        }
        data[attribute] = value
    }
    return data
}

// todo 以下是待完成的登录和注册功能
const login = (data) => {

}
const regis = (data) => {

}

const commit = (branch) => {
    let data = get_form_data()
    console.log(data)
    if (data.msg) {
        alert(data.msg)
        return
    }
    const request = branch === 0 ? login(data) : regis(data)


}
</script>
</html>